home *** CD-ROM | disk | FTP | other *** search
- unit OleRichEdit;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ComCtrls;
-
- type
- TOleRichEdit = class(TRichEdit)
- protected
- procedure CreateHandle; override;
- end;
-
- procedure Register;
-
- implementation
-
- {$B-}
- uses
- RichEdit, ActiveX, OleCtnrs, ComObj, Menus;
-
- type
- IRichEditOleCallback = interface(IUnknown)
- ['{00020D03-0000-0000-C000-000000000046}']
- function GetNewStorage: IStorage; safecall;
- procedure GetInPlaceContext(out Frame: IOleInPlaceFrame;
- out Doc: IOleInPlaceUIWindow; var FrameInfo: TOleInPlaceFrameInfo); safecall;
- procedure ShowContainerUI(fShow: Bool); safecall;
- procedure QueryInsertObject(const ClsID: TCLSID; Stg: IStorage; CP: Longint); safecall;
- procedure DeleteObject(OleObj: IOleObject); safecall;
- procedure QueryAcceptData(dataobj: IDataObject; var cfFormat: TClipFormat;
- reCO: DWord; fReally: Bool; hMetaPict: HGlobal); safecall;
- function ContextSensitiveHelp(fEnterMode: Bool): HResult; stdcall;
- function GetClipboardData(const ChRg: TCharRange; reCO: DWord; out DataObj: IDataObject): HResult; stdcall;
- procedure GetDragDropEffect(fDrag: Bool; grfKeyState: DWord;
- var dwEffect: DWord); safecall;
- procedure GetContextMenu(SelType: Word; OleObj: IOleObject;
- const ChRg: TCharRange; var Menu: HMenu); safecall;
- end;
-
- TRichEditOleCallback = class(TInterfacedObject, IRichEditOleCallback)
- private
- FOwner: TRichEdit;
- protected
- { IRichEditOleCallback }
- function GetNewStorage: IStorage; safecall;
- procedure GetInPlaceContext(out Frame: IOleInPlaceFrame;
- out Doc: IOleInPlaceUIWindow; var FrameInfo: TOleInPlaceFrameInfo); safecall;
- procedure ShowContainerUI(fShow: Bool); safecall;
- procedure QueryInsertObject(const ClsID: TCLSID; Stg: IStorage; CP: Longint); safecall;
- procedure DeleteObject(OleObj: IOleObject); safecall;
- procedure QueryAcceptData(dataobj: IDataObject; var cfFormat: TClipFormat;
- reCO: DWord; fReally: Bool; hMetaPict: HGlobal); safecall;
- function ContextSensitiveHelp(fEnterMode: Bool): HResult; stdcall;
- function GetClipboardData(const ChRg: TCharRange; reCO: DWord; out DataObj: IDataObject): HResult; stdcall;
- procedure GetDragDropEffect(fDrag: Bool; grfKeyState: DWord;
- var dwEffect: DWord); safecall;
- procedure GetContextMenu(SelType: Word; OleObj: IOleObject;
- const ChRg: TCharRange; var Menu: HMenu); safecall;
- public
- constructor Create(Owner: TRichEdit);
- destructor Destroy; override;
- end;
-
- { TRichEditOleCallback }
-
- constructor TRichEditOleCallback.Create(Owner: TRichEdit);
- begin
- inherited Create;
- FOwner := Owner
- end;
-
- destructor TRichEditOleCallback.Destroy;
- var
- Form: TCustomForm;
- begin
- Form := GetParentForm(FOwner);
- if Assigned(Form) and Assigned(Form.OleFormObject) then
- (Form.OleFormObject as IOleInPlaceUIWindow).SetActiveObject(nil, nil);
- inherited;
- end;
-
- function TRichEditOleCallback.ContextSensitiveHelp(fEnterMode: Bool): HResult;
- begin
- Result := E_NOTIMPL
- end;
-
- procedure TRichEditOleCallback.DeleteObject(OleObj: IOleObject);
- begin
- OleObj.Close(OLECLOSE_NOSAVE)
- end;
-
- function TRichEditOleCallback.GetClipboardData(const ChRg: TCharRange; reCO: DWord; out DataObj: IDataObject): HResult;
- begin
- Result := E_NOTIMPL
- end;
-
- procedure TRichEditOleCallback.GetContextMenu(SelType: Word;
- OleObj: IOleObject; const ChRg: TCharRange; var Menu: HMenu);
- begin
- Menu := 0
- end;
-
- procedure TRichEditOleCallback.GetDragDropEffect(fDrag: Bool;
- grfKeyState: DWord; var dwEffect: DWord);
- begin
- //Use normal effect (stored in dwEffect)
- end;
-
- procedure TRichEditOleCallback.GetInPlaceContext(
- out Frame: IOleInPlaceFrame; out Doc: IOleInPlaceUIWindow;
- var FrameInfo: TOleInPlaceFrameInfo);
- var
- Form: TCustomForm;
- begin
- //Get richedit's underlying form
- Form := ValidParentForm(FOwner);
- //Ensure there is a TOleForm object
- if Form.OleFormObject = nil then
- TOleForm.Create(Form);
- //Get relevant frame interface
- Frame := Form.OleFormObject as IOleInPlaceFrame;
- Doc := nil; //Document window is same as frame window
- FrameInfo.hWndFrame := Form.Handle;
- FrameInfo.fMDIApp := False;
- FrameInfo.hAccel := 0;
- FrameInfo.cAccelEntries := 0;
- end;
-
- function TRichEditOleCallback.GetNewStorage: IStorage;
- var
- LockBytes: ILockBytes;
- begin
- //Basically copied from TOleContainer.CreateStorage
- OleCheck(CreateILockBytesOnHGlobal(0, True, LockBytes));
- OleCheck(StgCreateDocfileOnILockBytes(LockBytes,
- STGM_READWRITE or STGM_SHARE_EXCLUSIVE or STGM_CREATE, 0, Result));
- end;
-
- procedure TRichEditOleCallback.QueryAcceptData(dataobj: IDataObject;
- var cfFormat: TClipFormat; reCO: DWord; fReally: Bool;
- hMetaPict: HGlobal);
- begin
- //Accept anything
- end;
-
- procedure TRichEditOleCallback.QueryInsertObject(const ClsID: TCLSID;
- Stg: IStorage; CP: Integer);
- begin
- //Accept anything
- end;
-
- procedure TRichEditOleCallback.ShowContainerUI(fShow: Bool);
- var
- Form: TCustomForm;
- begin
- if fShow then
- begin
- Form := GetParentForm(FOwner);
- if Assigned(Form) and Assigned(Form.Menu) then
- begin
- //Disassociate OLE menu handle from UI menu
- Form.Menu.SetOle2MenuHandle(0);
- //Make sure any space that was made for in-place toolbars is reclaimed
- (Form.OleFormObject as IVCLFrameForm).ClearBorderSpace
- end
- end
- end;
-
- { TOleRichEdit }
-
- procedure TOleRichEdit.CreateHandle;
- begin
- inherited;
- Perform(em_SetOleCallback, 0,
- Longint(TRichEditOleCallback.Create(Self) as IRichEditOleCallback))
- end;
-
- procedure Register;
- begin
- RegisterComponents('Clinic', [TOleRichEdit]);
- end;
-
- end.
-